package view;

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Composite;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.font.FontRenderContext;
import java.awt.geom.Rectangle2D;
import java.util.Observable;
import java.util.Observer;

import javax.swing.ImageIcon;
import javax.swing.JPanel;

import model.Chess;
import model.ChessArray;
import model.Prompt;
import util.SocketUtil;

public class GridPanel extends JPanel implements Observer {
	private Prompt prompt;
	private static final long serialVersionUID = 1L;
	private int chessw, chessh, left, top;
	private ChessArray chessarray;
	public boolean letgo=false;

	public GridPanel() {
		setBackground(new Color(0xB2DFEE));
		setOpaque(false);
		prompt = new Prompt();
		prompt.setPrompt("请点击开始", 48, Color.BLACK, 1f);
		addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
					if(letgo==false)return;
					super.mouseClicked(e);
					int j = ((e.getX()) - left) / chessw;
					int i = ((e.getY()) - left) / chessh;
					Chess chess = new Chess();
					chess.setX(j);
					chess.setY(i);
					chess.setccolor(SocketUtil.getccolor());
					if(chessarray.saveChess(chess)) 
					{
						letgo=false;						
						SocketUtil.sendMessage(chess);
						repaint();
					}
				}
		});
	}

	public GridPanel(ChessArray chessarray) {
		this();
		this.chessarray = chessarray;
	}

	@Override
	public void paintComponent(Graphics g) {
		// TODO Auto-generated method stub
		super.paintComponent(g);
		Graphics2D g2 = (Graphics2D) g;
		Composite composite = g2.getComposite();
		g2.setColor(new Color(0xB2DFEE));
		g2.setComposite(AlphaComposite.SrcOver.derive(0.8f));
		g2.fillRect(0, 0, getWidth(), getHeight());
		g2.setComposite(composite);
		drawGrid(g2);
		drawprompt(g2, prompt);
		drawchesses((Graphics2D) g, chessarray);
	}

	private void drawGrid(Graphics2D g2) {
		int width = getWidth();
		int height = getHeight();
		chessw = width / 15;
		chessh = height / 15;
		chessw = width / 15;
		chessh = height / 15;
		left = width % 15 / 2;
		top = height % 15 / 2;

		int bw = (width % 15 + chessw) / 2, bh = (height % 15 + chessh) / 2;
		int x = bw, y = bh;
		g2.setColor(Color.black);
		for (int i = 0; i < 15; i++) {
			g2.drawLine(x + i * chessw, y, x + i * chessw, height - bh);
			g2.drawLine(x, y + i * chessh, width - bw, bh + i * chessh);
		}
	}

	public Prompt getPrompt() {
		return prompt;
	}

	private void drawprompt(Graphics2D g, Prompt prompt) {
		Composite composite = g.getComposite();
		g.setColor(prompt.getColor());
		g.setComposite(AlphaComposite.SrcOver.derive(prompt.getTrans()));
		Font font = new Font("微软雅黑", Font.TRUETYPE_FONT, 48);
		g.setFont(font);
		FontRenderContext frc = g.getFontRenderContext();
		Rectangle2D rt = font.getStringBounds(prompt.getMessage(), frc);
		g.drawString(prompt.getMessage(), (int) (getWidth() - rt.getWidth()) / 2,
				(int) (getHeight() - rt.getHeight()) / 2);
		g.setComposite(composite);
	}

	private void drawchesses(Graphics2D g, ChessArray chessarray) {
		Composite composite = g.getComposite();
		for (int i = 0; i < 15; i++) {
			for (int j = 0; j < 15; j++) {
				if (chessarray.getChess(i, j) != null) {
					if (chessarray.getChess(i, j).equalColor(Color.BLACK)) {
						Image image = (new ImageIcon(getClass().getResource("../image/blackChessman.png")).getImage());
						g.drawImage(image, chessarray.getChess(i, j).getX() * chessw + left,
								chessarray.getChess(i, j).getY() * chessh + top, chessw, chessh, this);
					} else {
						Image image = (new ImageIcon(getClass().getResource("../image/whiteChessman.png")).getImage());
						g.drawImage(image, chessarray.getChess(i, j).getX() * chessw + left,
								chessarray.getChess(i, j).getY() * chessh + top, chessw, chessh, this);
					}
				}
			}
		}
		repaint();
		g.setComposite(composite);
	}

	@Override
	public void update(Observable arg0, Object arg1) {
		if (arg1 instanceof Chess) {
			Chess chess = (Chess) arg1;
			chessarray.saveChess(chess);
			letgo = true;
			repaint();
		}

	}
}
